package com.guigu.www.luolujin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.luolujin.Service.LljFoodZongService;
import com.guigu.www.mapper.FoodInnerMapper;
import com.guigu.www.mapper.FoodZongMapper;
import com.guigu.www.mapper.TemplateFoodMapper;
import com.guigu.www.pojo.FoodInner;
import com.guigu.www.pojo.FoodZong;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.pojo.TemplateFood;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作者 罗露金
 */
@Service
public class LljFoodZongServiceImpl implements LljFoodZongService {
    @Autowired
    FoodZongMapper mapper;
    @Autowired
    TemplateFoodMapper foodMapper;
    @Autowired
    FoodInnerMapper innerMapper;
    @Override
    public PageVo<FoodZong> pageFoodZong(Integer pageNum, Integer pageSize, String foodZongTitle) {
        QueryWrapper<FoodZong> wrapper = new QueryWrapper<>();
        wrapper.like("food_zong_title",foodZongTitle);
        Page page = new Page(pageNum, pageSize);
        IPage iPage = mapper.selectPage(page, wrapper);
        PageVo<FoodZong> pageVo = new PageVo<>();
        pageVo.setRows(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        return pageVo;
    }

    @Override
    public void updateFoodZong(Integer foodZongId, Integer foodZongState) {
        FoodZong foodZong=new FoodZong();
        foodZong.setFoodZongId(foodZongId);
        foodZong.setFoodZongState(foodZongState);
        mapper.updateById(foodZong);
    }

    @Override
    public List<TemplateFood> queryAllFood() {
        QueryWrapper<TemplateFood> wrapper = new QueryWrapper<>();
        wrapper.eq("temp_food_state ",0);
        return foodMapper.selectList(wrapper);
    }

    @Override
    public void insertFoodZong(FoodZong foodZong, Integer[] tempFoodId) {
        mapper.insert(foodZong);
        for (Integer foodId : tempFoodId) {
            FoodInner foodInner = new FoodInner(null,foodZong.getFoodZongId(),foodId);
            innerMapper.insert(foodInner);
        }
    }
}
